<?php

/**
 * This is the model class for table "provider_goods_cate".
 *
 * The followings are the available columns in table 'provider_goods_cate':
 * @property integer $id ID
 * @property string $name 分类名
 * @property integer $left_num Left Num
 * @property integer $right_num Right Num
 * @property integer $parent_id Parent
 * @property integer $layer Layer
 * @property integer $worker_id 所属用户
 */
class ProviderGoodsCate extends ActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return ProviderGoodsCate the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	public function getMdcClass() {
		return 'application.models.metadata.ProviderGoodsCateMetadata';
	}
	
	public function ownerCate() {
		if (Yii::app()->adminUser->role == AdminUser::ROLE_PROVIDER) {
			$criteria = $this->getDbCriteria();
			$criteria->addCondition('layer = 1 OR worker_id = :workerId');
			$criteria->params = array(
				':workerId' => Yii::app()->adminUser->id
			);
		}
	}
	
	protected function beforeValidate() {
		$this->parent_id = empty($this->parent_id) ? 1 : $this->parent_id;
		
		return parent::beforeValidate();
	}
}